Page({
  data: {
    pageShow: false,
    targetWidth: 1000,
    targetHeight: 2000,
    originWidth:0,
    uploadPic: [],
    tempFilePaths: []
  },

  // 点击加_压缩
  takePhoto() {
    var that = this;
    let imgViewList = that.data.imgViewList; //这个是用来承载页面循环展示图片的
    //拍照、从相册选择上传
    wx.chooseImage({
      count: 9,    //这个是上传的最大数量，默认为9
      sizeType: [''],    //这个可以理解为上传的图片质量类型（官方给的），虽然没什么卵用，要不然还要我们自己写压缩做什么
      sourceType: ['album', 'camera'],    //这个是图片来源，相册或者相机
      success(res) {
        //console.log(res)
        var tempFilePaths = res.tempFilePaths    //这个是选择后返回的图片列表
        that.getCanvasImg(0, tempFilePaths);    //进行压缩
      }
    });
  },
  //压缩并获取图片，这里用了递归的方法来解决canvas的draw方法延时的问题
  getCanvasImg(index, tempFilePaths) {
    var that = this
    if (index < tempFilePaths.length) {/////对每一个进行循环，获取连接后写入data
      wx.getImageInfo({
        src: tempFilePaths[index],
        success(res) {

          var targetWidth = 0
          var targetHeight = 0
          var originHeight = res.height
          var originWidth = res.width
         that.setData({
            originWidth:originWidth
          })
          var maxWidth = 400
          var maxHeight = 900 //2100以内 按宽度1000，高度自适应压缩，

          if (originWidth > maxWidth || originHeight > maxHeight) { //判断是否压缩，
            if (originWidth / originHeight > maxWidth / maxHeight) {
              targetWidth = maxWidth;
              targetHeight = Math.round(maxWidth * (originHeight / originWidth));
            } else {
              targetHeight = maxHeight;
              targetWidth = Math.round(maxHeight * (originWidth / originHeight));
            }
            const ctx = wx.createCanvasContext('attendCanvasId');
            ctx.drawImage(tempFilePaths[index], 0, 0, targetWidth, targetHeight);
            ctx.draw(true, function () {
              wx.canvasToTempFilePath({
                canvasId: 'attendCanvasId',
                destWidth: targetWidth,
                destHeight: targetHeight,
                success(res) {
                  that.setData({
                    tempFilePaths: that.data.tempFilePaths.concat(res.tempFilePath)
                  })
                  that.getCanvasImg(index, tempFilePaths);
                  //console.log(res)
                }
              })
              index++
            })
          } else {//不需要压缩
            that.setData({
              tempFilePaths: that.data.tempFilePaths.concat(tempFilePaths[index])
            })
            index++
          }
        }
      })
    }
  },



  //   //上传图片
  //   uploadCanvasImg: function (canvasImg){
  //     var that = this;
  //     let imgViewList = that.data.imgViewList;
  //     var tempImg = canvasImg;
  //     wx.uploadFile({
  //       url: app.d.fileServer,//文件服务器的地址
  //       filePath: tempImg,
  //       formData: {
  //         paramPath: "gift"
  //       },
  //       name: 'file',
  //       success: function (res) {
  //         var json2map = JSON.parse(res.data);
  //         imgViewList.push(app.d.imageUrlFix + json2map[0].fileUrl);
  //         that.setData({
  //           imgViewList: imgViewList,
  //         })
  //       }
  //     })
  //   },

  onLoad() {
    this.setData({
      pageShow: true,
    })
  }


})